package dd.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

import xml.Message;
import dd.model.Event;
import dd.model.Model;
import dd.view.CreateForm;
import dd.view.SignInScreen;

public class SignInButtonController implements ActionListener {
	/**
	 * call when user at the sign in screen click sign in button get the information on the screen which is username and
	 * password send the information to the server(difference between user and moderator)
	 */

	SignInScreen signin;
	Event event;

	public SignInButtonController(SignInScreen sis, Event event) {
		signin = sis;
		this.event = event;
	}

	public void actionPerformed(ActionEvent arg0) {
		JTextField usernameScreen = signin.getUserNameTextField();
		JTextField passwordScreen = signin.getPasswordTextField();
		String username = usernameScreen.getText();
		String password = passwordScreen.getText();
		// Model model = Model.getInstance();
		// Event e = model.getEvent();
		System.out.println("id want to login:" + event.getEventID());
		// send message depending on is this is moderator
		if (event.isModSignIn()) {
			event.setUserName(username);
			event.setPassword(password);
			CreateForm creatform = new CreateForm(event);
			creatform.setVisible(true);
			signin.dispose();
		} else {
			// set signinscreen to model for furthur use
			Model.getInstance().setSignInScreen(signin);
			event.setUserName(username);
			event.setPassword(password);
			String xmlString = Message.requestHeader() + "<signInRequest id='" + event.getEventID() + "'>"
					+ "<user name='" + username + "' password='" + password + "'/>" + "</signInRequest>" + "</request>";
			Message m = new Message(xmlString);
			Model.getInstance().getServerAccess().sendRequest(m);
		}

		/*
		 * if(event.getUserName().equals(username) && event.getPassword().equals(password)) { event.setUserStatus();
		 * if(event.getMode().equals("Open")) { addChoiceScreen addchoiceScreen = new addChoiceScreen(event);
		 * addchoiceScreen.setVisible(true); } else { addEdgesScreen addES = new addEdgesScreen(event);
		 * addES.setVisible(true); } signin.dispose(); } else {
		 * System.out.println("User name or password is not correct, please check again!"); }
		 * 
		 * if(event.getModName().equals(username) && event.getModPass().equals(password)) { event.setModStatus();
		 * CreateForm creatform = new CreateForm(event); creatform.setVisible(true); signin.dispose(); } else {
		 * System.out.println("User name or password is not correct, please check again!"); }
		 */

	}

}
